/*
 * Created on Jul 14, 2005
 *
 */
package com.erclab.internal.xpresso.handlers;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import org.apache.commons.dbutils.ResultSetHandler;

import com.erclab.internal.xpresso.reports.ReportFilter;

/**
 * @author Enrique Rodriguez.
 *
 */
public class ReportFilterHandler implements ResultSetHandler {

	private void setFilterProperties(ReportFilter f, ResultSet rs) throws SQLException{
		
		f.setIdFilter(rs.getString("IdFilter"));
		f.setClassHandler(rs.getString("ClassHandler"));
		f.setColumnToFilter(rs.getString("ColumnToFilter"));
		f.setLabel(rs.getString("Label"));
		f.setParameters(rs.getString("Parameters"));
		f.setLookupDataSourceId(rs.getInt("LookupDataSourceId"));
		f.setLookupQuery(rs.getString("LookupQuery"));
		f.setStyle(rs.getString("Style"));
		f.setFlexClass(rs.getString("FlexClass"));
	}

	/* (non-Javadoc)
	 * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
	 */
	public Object handle(ResultSet rs) throws SQLException {
		ArrayList<ReportFilter> filters = new ArrayList<ReportFilter>();
		while (rs.next()){
			String classHandler = rs.getString("classHandler");
			ReportFilter f = null;
			try {
				Class filterClass = Class.forName(classHandler);
				f = (ReportFilter) filterClass.newInstance();
				// Set values of the form element by calling the setters
				this.setFilterProperties(f, rs);
			} catch(ClassNotFoundException e){
				// TODO: Handle Exception
				System.out.println(e.toString());
			} catch(IllegalAccessException e){
//				TODO: Handle Exception
				System.out.println(e.toString());
			} catch(InstantiationException e){
//				TODO: Handle Exception
				System.out.println(e.toString());
			}

			filters.add(f);
				
		}
		return filters;	
	}

}
